﻿using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace Marmat.Forms
{
    partial class Helper
    {
        public class UITypeEditor
        {
            public static void ComponentChangeService(Control Component, string PropertyName, bool Save)
            {
                IDesignerHost designerHost = Component.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
                var designerTransaction = designerHost.CreateTransaction();
                var member = TypeDescriptor.GetProperties(Component)[PropertyName];
                IComponentChangeService componentChangeService = Component.Site.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                if (Save)
                {
                    componentChangeService.OnComponentChanged(Component, member, null, null);
                    designerTransaction.Commit();
                }
                else
                {
                    designerTransaction.Cancel();
                }
            }

            public static void ComponentChangeService(Component Component, string PropertyName, bool Save)
            {
                IDesignerHost designerHost = Component.Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
                var designerTransaction = designerHost.CreateTransaction();
                var member = TypeDescriptor.GetProperties(Component)[PropertyName];
                IComponentChangeService componentChangeService = Component.Site.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                if (Save)
                {
                    componentChangeService.OnComponentChanged(Component, member, null, null);
                    designerTransaction.Commit();
                }
                else
                {
                    designerTransaction.Cancel();
                }
            }
        }
    }
}